package engine.logic.login
{
	import com.greensock.TweenLite;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	/**
	 * 封装Timer延时调用细节，防止延时不移除等 
	 * 一个NxTimer只管理一个函数，可以重复调用timingCall以改变被调用函数，而不用担心上一个函数定时调用不移除
	 * @author guyi
	 */	
	public class NxTimer
	{
		
//		static private var _instanceCount:int=0;
		static private var _activeInstanceCount:int	=0;
		
		private var _timer	: Timer;
		private var _delay	: int;
		private var _isDrawnAtOnce:Boolean	=	false;
		
		
		private var _targetFunc:Function;
		private var _params:Array;
		
		public function NxTimer()
		{
		}
		
		/**
		 *定时调用，从添加时开始调用  
		 * @param func 被调用函数
		 * @param params 被调用函数的参数
		 * @param delay 事件间隔 单位ms，非法延时将自动设定为1000ms
		 * @param repeatCount 0为不自动停止，非法次数自动设为0
		 * 
		 */	
		public function timingCall(func:Function,params:Array=null,delay:int=1000,repeatCount:int=0,isDrawnAtOnce:Boolean=false):void
		{
			if(	null	==	_timer)
					_timer	=	new Timer(500);
			
			this.stop();
			
			_isDrawnAtOnce		=	isDrawnAtOnce;
			_timer.delay		=	delay	<	1	? 1000	:	delay;
			_timer.repeatCount	=	repeatCount	<0	?	0	:repeatCount;
			
			_timer.addEventListener(TimerEvent.TIMER,timerHandler);
			_timer.start();
			
			_targetFunc	=	func;
			_params		=	params;
			
			
			_activeInstanceCount++;
		}
		
		/**
		 *停止调用 
		 */		
		public function stop():void
		{
			if(	null	!=	_timer)
			{
				_timer.stop();
				if(_timer.hasEventListener(TimerEvent.TIMER))
				{
					_timer.removeEventListener(TimerEvent.TIMER,timerHandler);
					_activeInstanceCount--;
				}
			}
			
			_targetFunc	=	null;
			_params		=	null;
		}
		
		private function timerHandler(e:TimerEvent):void
		{
			if(	null	!=	_targetFunc)
				_targetFunc.apply(null,_params);
			
			if(_isDrawnAtOnce)
			{
				e.updateAfterEvent();
			}
		}
		
	}
}